#coding=utf-8
import wx
import sys
from os import path
from getopt import getopt
from wx.lib.flashwin import FlashWindow

reload(sys)
sys.setdefaultencoding('utf-8')

class MyFrame(wx.Frame):
    INTERVAL = 50

    def __init__(self, fullscreen = False):
        self.rawtitle = 'Swift'
        wx.Frame.__init__(self, None, title=self.rawtitle,
                          pos=(100, 0), size=(800, 500))
        self.fullscreen = fullscreen

        # Lay out the controls
        self.panel = wx.Panel(self)
        sizer = wx.BoxSizer()
        self.panel.SetSizer(sizer)

        self.flash = FlashWindow(self.panel)
        sizer.Add(self.flash, 1, wx.EXPAND)

        sizer.Layout()
        self.panel.Fit()

        # A trick to prevent FlashWindow from cracking
        tmp = wx.TextCtrl(self.panel)
        tmp.SetFocus()
        tmp.Hide()

        # Layout the menu
        menubar = self.menubar = wx.MenuBar()

        menuOpen = wx.Menu()
        menuOpen.__name__ = 'open'
        menubar.Append(menuOpen, '打开')

        menuMin = wx.Menu()
        menuMin.__name__ = 'min'
        menubar.Append(menuMin, '最小化')

        menuQuit = wx.Menu()
        menuQuit.__name__ = 'quit'
        menubar.Append(menuQuit, '退出')

        self.Bind(wx.EVT_MENU_OPEN, self.OnMenuOpen)
        self.SetMenuBar(menubar)

        # Set the timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer)

    def OnMenuOpen(self, event):
        menu = event.GetMenu()
        if not hasattr(menu, '__name__'): return
        name = menu.__name__
        print 'Menu clicked:', name
        if name == 'quit':
            sys.exit(0)
        elif name == 'open':
            dlg = wx.FileDialog(self, message='打开 Flash 文件...',
                     wildcard='Flash 文件(*.swf)|*.swf', style=wx.OPEN)
            if dlg.ShowModal() == wx.ID_OK:
                self.Load(dlg.GetPath())
        elif name == 'min':
        	self.Iconize()

    def OnTimer(self, event):
        title = self.rawtitle + ' ... {%d/%d}' %\
                                (self.flash.CurrentFrame(), self.totalframes)
        self.SetTitle(title)

    def Load(self, swf):
        print 'Loading flash file:', swf
        if path.exists(swf):
            self.flash.LoadMovie(0, 'file://' + swf)
            self.totalframes = self.flash.totalframes

            self.rawtitle = 'Swift - [%s]' % swf
            self.SetTitle(self.rawtitle)

            self.timer.Start(self.INTERVAL)


class MyApp(wx.App):

    def __init__(self, redirect = True, swf = '', fullscreen = False):
        self.swf = swf
        self.fullscreen = fullscreen
        wx.App.__init__(self, redirect)

    def OnInit(self):
        self.frame = MyFrame(fullscreen)
        self.SetTopWindow(self.frame)
        if self.frame.fullscreen:
            self.frame.ShowFullScreen(True,
                       style=wx.FULLSCREEN_ALL ^ wx.FULLSCREEN_NOMENUBAR)
        else:
            self.frame.Show()

        if self.swf:
            self.frame.Load(self.swf)

        del self.fullscreen
        del self.swf
        return True


def main(swf = '', fullscreen=False, redirect=True):
    app = MyApp(redirect, swf, fullscreen)
    app.MainLoop()

if __name__ == '__main__':
    swf = ''
    fullscreen = False
    try:
        opts, args = getopt(sys.argv[1:], 'f', ['fullscreen'])
        for opt, arg in opts:
            if opt in ('-f', '--fulscreen'):
                fullscreen = True
                print 'Fullscreen turned on'

        for arg in args:
            if path.exists(arg) and path.splitext(arg)[1] == '.swf':
                swf = arg
                print 'Recognized swf from command line: ', swf
                break
    except:
        pass

    main(swf, fullscreen, False)
